package cn.net.qinling.xmlaccess;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DB2DataAccess extends JDBCDataAccess {


	public DB2DataAccess() throws SQLException {
		DriverManager.registerDriver(new com.ibm.db2.jcc.DB2Driver());
		this.conn = DriverManager.getConnection(DBConfiguration.DB2_DB_URL,
				DBConfiguration.DB2_DB_USERNAME,
				DBConfiguration.DB2_DB_PASSWORD);
	}

	@Override
	public void insert(String uid, String content) {
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(INSERT_SQL);
			stmt.setString(1, uid);
			stmt.setString(2, content);

			stmt.execute();
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}
	}

	@Override
	public String getRecordById(String uid) {
		try {
			String sql = String.format(QUERY_RECORD_UID, uid);
			PreparedStatement stmt = this.conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				return rs.getString(2);
			}
			stmt.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return null;
	}

}
